﻿using LogLib;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using AppDataStruct;

namespace EECMISWS
{
    internal partial class WSClient
    {
        public bool MakeBarCode(string prefix,out string barCode)
        {
            barCode = "";
            JObject Args = new JObject();
            Args["prefix"] = prefix;
            string json = Args.ToString();
            WebReturnArgs mReturn = new WebReturnArgs();
            mReturn = CallUnimethod("sq_makeBarcode", json, true);
            if (!mReturn.CallSuccessed)
            {
                Log.WriteAsync(mReturn.ExceptionString);
                return false;
            }
            if (!mReturn.Success)
            {
                _strMessage = string.Format("保存预受理信息错误.\n 详情: 返回代码{0},{1}",
                    mReturn.ErrorCode,
                    mReturn.Message);
                Log.WriteAsync(_strMessage);
                return false;
            }
            if (string.IsNullOrEmpty(mReturn.DataContent))
            {
                mReturn.CustomCode = 702;
                _strMessage = string.Format("获取业务编号内容为空.\n 详情: 返回{0},{1}",
                 mReturn.ErrorCode,
                 mReturn.Message);                
                Log.WriteAsync(_strMessage);
                return false;
            }
            JObject jObj = JObject.Parse(mReturn.DataContent);
            if (jObj["barcode"] == null)
            {             
                mReturn.CustomCode = 702;
                _strMessage = "获取业务编号内容为空!" + mReturn.DataContent;            
                Log.WriteAsync(_strMessage);
                return false;
            }
            barCode = jObj["barcode"].ToString();
            return true;
        }

        /// <summary>
        /// 保存预受理信息
        /// </summary>
        /// <param name="xmlPath"></param>
        /// <returns></returns>
        public bool SavePreApply(PreApplyInfo appInfo)
        {   
            JObject jsonObj = new JObject();
            string[] jsonArray = null;
            bool bRet = Data2Json.ClassToJson(appInfo, ClientConfig.TBDWBH, out jsonArray);
            if (!bRet)
            {
                _strMessage = jsonArray[0];
                return false;
            }
            try
            {
                foreach (string json in jsonArray)
                {
                    if (json == "")
                        continue;
                    Log.WriteAsync("send：" + "[" + json + "]");
                    WebReturnArgs mReturn = new WebReturnArgs();
                    mReturn = CallUnimethod("sq_savePreApply", json, false);
                    if (!mReturn.CallSuccessed)
                    {                      
                        Log.WriteAsync(mReturn.ExceptionString);
                        return false;
                    }
                    if (!mReturn.Success)
                    {
                        _strMessage = string.Format("保存预受理信息错误.\n 详情: 返回代码{0},{1}",
                            mReturn.ErrorCode,
                            mReturn.Message);                      
                        Log.WriteAsync(_strMessage);
                        return false;
                    }
                }              
            }
            catch (Exception ex)
            {
                Log.WriteAsync("预受理信息入库时发生异常:" + ex.Message);
                return false;
            }
            return true;
        }

        /// <summary>
        /// 生物特征信息入库 
        /// </summary>
        /// <param name="sfzh"></param>
        /// <returns></returns>  
        public bool SaveBioIdent(string xmlPath)
        {          
            string[] jsonArray = null;
            string jsonHandWriting = null;
            string[] jsonjsonXCRXArray = null;
            bool bRet = Data2Json.BioIdent2Json(xmlPath, ClientConfig.TBDWBH, out jsonArray, out jsonHandWriting, out jsonjsonXCRXArray);
            if (!bRet)
            {
                _strMessage = jsonArray[0];
                Log.WriteAsync("保存生物信息失败:" + _strMessage);
                return false;
            }
            try
            {
                if (!string.IsNullOrEmpty(jsonHandWriting)) 
                {
                    WebReturnArgs mReturn = new WebReturnArgs();
                    mReturn = CallUnimethod("sq_saveHandwriting", jsonHandWriting, false);
                    if (!mReturn.CallSuccessed)
                    {
                        _strMessage = "上传手写签名信息发生了异常:" + mReturn.ExceptionString;
                       Log.WriteAsync(_strMessage);
                        return false;
                    }
                    if (!mReturn.Success)
                    {
                        _strMessage = string.Format("上传手写签名信息失败.\n 详情: 返回代码{0},{1}",
                            mReturn.ErrorCode,
                            mReturn.Message);
                        Log.WriteAsync(_strMessage);
                        return false;
                    }
                }
                foreach (string jsonXCRX in jsonjsonXCRXArray)
                {
                    if (jsonXCRX == "")
                        continue;
                    WebReturnArgs mReturn = new WebReturnArgs();
                    mReturn = CallUnimethod("sq_saveLiveportrait", jsonXCRX, false);
                    if (!mReturn.CallSuccessed)
                    {
                        _strMessage = "上传现场人像发生了异常:" + mReturn.ExceptionString;
                        Log.WriteAsync(_strMessage);
                        return false;
                    }
                    if (!mReturn.Success)
                    {
                        _strMessage = string.Format("上传现场人像失败.\n 详情: 返回代码{0},{1}",
                            mReturn.ErrorCode,
                            mReturn.Message);
                        Log.WriteAsync(_strMessage);
                        return false;
                    }                   
                }
                foreach (string json in jsonArray)
                {
                    if (json == "")
                        continue;
                    WebReturnArgs mReturn = new WebReturnArgs();
                    mReturn = CallUnimethod("sq_saveBioIdent", json, false);
                    if (!mReturn.CallSuccessed)
                    {
                        _strMessage = "上传指纹发生了异常:" + mReturn.ExceptionString;
                        Log.WriteAsync(_strMessage);
                        return false;
                    }
                    if (!mReturn.Success)
                    {
                        _strMessage = string.Format("上传指纹失败.\n 详情: 返回代码{0},{1}",
                            mReturn.ErrorCode,
                            mReturn.Message);
                        Log.WriteAsync(_strMessage);
                        return false;
                    }                  
                }
            }
            catch (Exception ex)
            {
                _strMessage = "生物特征信息入库时发生异常:" + ex.Message;
                Log.WriteAsync(_strMessage);
                return false;
            }
            Log.WriteAsync("所有生物特征信息入库成功!");
            return true;
        }

        /// <summary>
        /// 上传电子档案
        /// </summary>
        /// <param name="ywbh">     业务编号 </param>
        /// <param name="FilePath"> 文件路径 </param>
        /// <param name="mode"> 模式. 1-覆盖模式, 2-追加模式 </param>
        /// <param name="WJM"> 文件名称 </param>
        /// <returns></returns>  
        public bool SaveSFZHKBPDF(PreApplyInfo appInfo)
        {           
            //string[] jsonArray = null;
            bool bRet = Data2Json.PDF2Json(appInfo,out List<string> jsonArray);
            if (!bRet)
            {
                _strMessage = "上传电子档案失败:" + jsonArray[0];
                Log.WriteAsync(_strMessage);
                return false;
            }
            try
            {
                foreach (string json in jsonArray)
                {
                    if (json == "")
                        continue;
                    WebReturnArgs mReturn = new WebReturnArgs();
                    //Log.WriteAsync($"准备调用da_saveEArchive，参数:[{json}]");
                    mReturn = CallUnimethod("da_saveEArchive", json, false);
                    if (!mReturn.CallSuccessed)
                    {
                        _strMessage = "电子档案入库发生了异常:" + mReturn.ExceptionString;
                        Log.WriteAsync(_strMessage);
                        return false;
                    }
                    if (!mReturn.Success)
                    {
                        _strMessage = string.Format("电子档案入库失败.\n 详情: 返回代码{0},{1}",
                            mReturn.ErrorCode,
                            mReturn.Message);
                        Log.WriteAsync(_strMessage);
                        return false;
                    }                
                }
            }
            catch (Exception ex)
            {
                _strMessage = "电子档案入库时发生异常：" + ex.Message;
                Log.WriteAsync(_strMessage);
                Log.WriteException(ex.ToString());
                return false;
            }
            Log.WriteAsync("电子档案入库成功");
            return true;
        }

        /// <summary>
        /// 单独上传手写签名 
        /// </summary>
        /// <param name="sfzh"></param>
        /// <returns></returns>  
        public bool SaveHandWriting(string xmlPath) 
        {
            string[] jsonArray = null;
            bool bRet = Data2Json.HandWriting2Json(xmlPath, ClientConfig.TBDWBH, out jsonArray);
            if (!bRet)
            {
                _strMessage = "上传手写签名失败:" + jsonArray[0];
                Log.WriteAsync(_strMessage);
                return false;
            }
            try
            {
                foreach (string json in jsonArray)
                {
                    if (json == "")
                        continue;

                    WebReturnArgs mReturn = new WebReturnArgs();
                    mReturn = CallUnimethod("sq_saveHandwriting", json, false);
                    if (!mReturn.CallSuccessed)
                    {
                        _strMessage = "上传手写签名信息发生了异常:" + mReturn.ExceptionString;
                        Log.WriteAsync(_strMessage);
                        return false;
                    }
                    if (!mReturn.Success)
                    {
                        _strMessage = string.Format("上传手写签名信息失败.\n 详情: 返回代码{0},{1}",
                            mReturn.ErrorCode,
                            mReturn.Message);
                        Log.WriteAsync(_strMessage);
                        return false;
                    }
                }
            }
            catch (Exception ex)
            {
                _strMessage = "上传手写签名信息时发生异常：" + ex.Message;
                Log.WriteAsync(_strMessage);
                Log.WriteException(ex.ToString());
                return false;
            
            }
            Log.WriteAsync("上传手写签名成功");
            return true;
        }

        /// <summary>
        /// 单独上传手写签名 
        /// </summary>
        /// <param name="sfzh"></param>
        /// <returns></returns>  
        public bool SaveHandWriting(string ywbh, string xcrx)
        {
            if (string.IsNullOrEmpty(ywbh))
            {
                _strMessage = "业务编号为空.";
                Log.WriteAsync(_strMessage);
                return false;
            }
            if (!File.Exists(xcrx))
            {
                _strMessage = "签名文件不存在.";
                Log.WriteAsync(_strMessage);
                return false;
            }
            try
            {
                byte[] bPic = Common.DatToByte(xcrx);
                if (bPic == null || bPic.Length == 0)
                {
                    _strMessage = "文件数据为空";
                    return false;
                }

                string strBase64string = Common.ByteToBase64String(bPic);
                if (string.IsNullOrEmpty(strBase64string.Trim()))
                {
                    _strMessage = "Base64数据为空";
                    return false;
                }

                JObject Args = new JObject();
                Args.Add("ywbh", ywbh);
                Args.Add("sxqm", strBase64string);
                string json = Args.ToString();
                WebReturnArgs mReturn = new WebReturnArgs();
                mReturn = CallUnimethod("sq_saveHandwriting", json, false);

                if (!mReturn.CallSuccessed)
                {
                    _strMessage = "上传手写签名信息发生了异常:" + mReturn.ExceptionString;
                    Log.WriteAsync(_strMessage);
                    return false;
                }
                if (!mReturn.Success)
                {
                    _strMessage = string.Format("上传手写签名信息失败.\n 详情: 返回代码{0},{1}",
                        mReturn.ErrorCode,
                        mReturn.Message);
                    Log.WriteAsync(_strMessage);
                    return false;
                }
                _strMessage = mReturn.Message;
                string strLog = string.Format("手写签名{0}{1}, 业务编号:{2}", xcrx, _strMessage, ywbh);
                Log.WriteAsync(strLog);
                return true;
            }
            catch (Exception ex)
            {
                _strMessage = "上传手写签名信息时发生异常：" + ex.Message;
                Log.WriteAsync(_strMessage);
                Log.WriteException(ex.ToString());
                return false;
            }     
        }

        /// <summary>
        /// 单独上传现场人像
        /// </summary>
        /// <param name="sfzh"></param>
        /// <returns></returns>  
        public bool SaveLiveportrait(string ywbh, string imgPath)
        {
            if (string.IsNullOrEmpty(ywbh))
            {
                _strMessage = "业务编号为空.";
                Log.WriteAsync(_strMessage);
                return false;
            }
            if (!File.Exists(imgPath))
            {
                _strMessage = "签名文件不存在.";
                Log.WriteAsync(_strMessage);
                return false;
            }
            try
            {
                byte[] bPic = Common.DatToByte(imgPath);
                if (bPic == null || bPic.Length == 0)
                {
                    _strMessage = "文件数据为空";
                    return false;
                }

                string strBase64string = Common.ByteToBase64String(bPic);
                if (string.IsNullOrEmpty(strBase64string.Trim()))
                {
                    _strMessage = "Base64数据为空";
                    return false;
                }

                JObject Args = new JObject();
                Args.Add("ywbh", ywbh);
                Args.Add("rxxx", strBase64string);
                string json = Args.ToString();
                WebReturnArgs mReturn = new WebReturnArgs();
                mReturn = CallUnimethod("sq_saveLiveportrait", json, false);

                if (!mReturn.CallSuccessed)
                {
                    _strMessage = "上传现场人像信息发生了异常:" + mReturn.ExceptionString;
                    Log.WriteAsync(_strMessage);
                    return false;
                }
                if (!mReturn.Success)
                {
                    _strMessage = string.Format("上传现场人像信息失败.\n 详情: 返回代码{0},{1}",
                        mReturn.ErrorCode,
                        mReturn.Message);
                    Log.WriteAsync(_strMessage);
                    return false;
                }
                _strMessage = mReturn.Message;
                string strLog = string.Format("现场人像{0}{1}, 业务编号:{2}", imgPath, _strMessage, ywbh);
                Log.WriteAsync(strLog);
                return true;
            }
            catch (Exception ex)
            {
                _strMessage = "上传现场人像信息时发生异常：" + ex.Message;
                Log.WriteAsync(_strMessage);
                Log.WriteException(ex.ToString());
                return false;
            }           
        }

        //单独上传指纹信息
        public bool SaveFingerPrint(PreApplyInfo appInfo)
        {         
            try
            {
                if (!string.IsNullOrEmpty(appInfo.HZSQ.YWBH)) 
                {
                    JObject Args = new JObject();
                    Args.Add("xpbh", appInfo.HZSQ.YWBH);
                    if (!string.IsNullOrEmpty(appInfo.HZSQ.WSQ))
                    {
                        Args.Add("wsqContext", appInfo.HZSQ.WSQ);
                        Args.Add("wsqName", appInfo.HZSQ.WSQ_File);
                    }

                    if (!string.IsNullOrEmpty(appInfo.HZSQ.TPL))
                    {
                        Args.Add("tplContext", appInfo.HZSQ.TPL);
                        Args.Add("tplName", appInfo.HZSQ.TPL_File);
                    }

                    if (!string.IsNullOrEmpty(appInfo.HZSQ.ERR))
                    {
                        Args.Add("errContext", appInfo.HZSQ.ERR);
                        Args.Add("errName", appInfo.HZSQ.ERR_File);
                    }

                    if (!string.IsNullOrEmpty(appInfo.HZSQ.MD5))
                    {
                        Args.Add("md5Context", appInfo.HZSQ.MD5);
                        Args.Add("md5Name", appInfo.HZSQ.MD5_File);
                    }

                    if (Args.Count > 1)
                    {
                        string json = Args.ToString();
                        WebReturnArgs mReturn = new WebReturnArgs();
                        mReturn = CallUnimethod("sq_saveBioIdent", json, false);

                        if (!mReturn.CallSuccessed)
                        {
                            _strMessage = "上传护照指纹信息发生了异常:" + mReturn.ExceptionString;
                            Log.WriteAsync(_strMessage);
                            return false;
                        }
                        if (!mReturn.Success)
                        {
                            _strMessage = string.Format("上传护照指纹信息失败.\n 详情: 返回代码{0},{1}",
                                mReturn.ErrorCode,
                                mReturn.Message);
                            Log.WriteAsync(_strMessage);
                            return false;
                        }
                        _strMessage = mReturn.Message;
                        string strLog = string.Format("护照指纹信息成功, 业务编号:{0}", appInfo.HZSQ.YWBH);
                        Log.WriteAsync(strLog);
                    }
                }

                if (!string.IsNullOrEmpty(appInfo.SCZSQ.YWBH))
                {
                    JObject Args = new JObject();
                    Args.Add("xpbh", appInfo.SCZSQ.YWBH);
                    if (!string.IsNullOrEmpty(appInfo.SCZSQ.WSQ))
                    {
                        Args.Add("wsqContext", appInfo.SCZSQ.WSQ);
                        Args.Add("wsqName", appInfo.SCZSQ.WSQ_File);
                    }

                    if (!string.IsNullOrEmpty(appInfo.SCZSQ.TPL))
                    {
                        Args.Add("tplContext", appInfo.SCZSQ.TPL);
                        Args.Add("tplName", appInfo.SCZSQ.TPL_File);
                    }

                    if (!string.IsNullOrEmpty(appInfo.SCZSQ.ERR))
                    {
                        Args.Add("errContext", appInfo.SCZSQ.ERR);
                        Args.Add("errName", appInfo.SCZSQ.ERR_File);
                    }

                    if (!string.IsNullOrEmpty(appInfo.SCZSQ.MD5))
                    {
                        Args.Add("md5Context", appInfo.SCZSQ.MD5);
                        Args.Add("md5Name", appInfo.SCZSQ.MD5_File);
                    }

                    if (Args.Count > 1)
                    {
                        string json = Args.ToString();
                        WebReturnArgs mReturn = new WebReturnArgs();
                        mReturn = CallUnimethod("sq_saveBioIdent", json, false);

                        if (!mReturn.CallSuccessed)
                        {
                            _strMessage = "上传港澳证指纹信息发生了异常:" + mReturn.ExceptionString;
                            Log.WriteAsync(_strMessage);
                            return false;
                        }
                        if (!mReturn.Success)
                        {
                            _strMessage = string.Format("上传港澳证指纹信息失败.\n 详情: 返回代码{0},{1}",
                                mReturn.ErrorCode,
                                mReturn.Message);
                            Log.WriteAsync(_strMessage);
                            return false;
                        }
                        _strMessage = mReturn.Message;
                        string strLog = string.Format("港澳证指纹信息成功, 业务编号:{0}", appInfo.SCZSQ.YWBH);
                        Log.WriteAsync(strLog);
                    }
                }

                if (!string.IsNullOrEmpty(appInfo.DLZSQ.YWBH))
                {
                    JObject Args = new JObject();
                    Args.Add("xpbh", appInfo.DLZSQ.YWBH);
                    if (!string.IsNullOrEmpty(appInfo.DLZSQ.WSQ))
                    {
                        Args.Add("wsqContext", appInfo.DLZSQ.WSQ);
                        Args.Add("wsqName", appInfo.DLZSQ.WSQ_File);
                    }

                    if (!string.IsNullOrEmpty(appInfo.DLZSQ.TPL))
                    {
                        Args.Add("tplContext", appInfo.DLZSQ.TPL);
                        Args.Add("tplName", appInfo.DLZSQ.TPL_File);
                    }

                    if (!string.IsNullOrEmpty(appInfo.DLZSQ.ERR))
                    {
                        Args.Add("errContext", appInfo.DLZSQ.ERR);
                        Args.Add("errName", appInfo.DLZSQ.ERR_File);
                    }

                    if (!string.IsNullOrEmpty(appInfo.DLZSQ.MD5))
                    {
                        Args.Add("md5Context", appInfo.DLZSQ.MD5);
                        Args.Add("md5Name", appInfo.DLZSQ.MD5_File);
                    }

                    if (Args.Count > 1)
                    {
                        string json = Args.ToString();
                        WebReturnArgs mReturn = new WebReturnArgs();
                        mReturn = CallUnimethod("sq_saveBioIdent", json, false);

                        if (!mReturn.CallSuccessed)
                        {
                            _strMessage = "上传大陆证指纹信息发生了异常:" + mReturn.ExceptionString;
                            Log.WriteAsync(_strMessage);
                            return false;
                        }
                        if (!mReturn.Success)
                        {
                            _strMessage = string.Format("上传大陆证指纹信息失败.\n 详情: 返回代码{0},{1}",
                                mReturn.ErrorCode,
                                mReturn.Message);
                            Log.WriteAsync(_strMessage);
                            return false;
                        }
                        _strMessage = mReturn.Message;
                        string strLog = string.Format("大陆证指纹信息成功, 业务编号:{0}", appInfo.DLZSQ.YWBH);
                        Log.WriteAsync(strLog);
                    }
                }
                
                return true;
            }
            catch (Exception ex)
            {
                _strMessage = "上传指纹信息时发生异常：" + ex.Message;
                Log.WriteAsync(_strMessage);
                Log.WriteException(ex.ToString());
                return false;
            }
        }
    }
}
